package hdelsof

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"novel-read-sys/cache"
	"novel-read-sys/models"
	"novel-read-sys/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	name := c.PostForm("name")
	passes := c.PostForm("passes")

	if name == "" || passes == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}

	redis_key := "redis_login" + name
	jsons, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if jsons >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次，禁止登录",
		})
		return
	}
	var user models.User
	models.DB.Where("name=?", name).First(&user)
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Passes != pkg.Md5(passes) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if jsons == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码输入错误密码3次，封禁 2 小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	are := c.PostForm("are")

	if title == "" || content == "" || are == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Ares{
		Title:   title,
		Content: content,
		Are:     are,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	Offset := (page - 1) * size

	var count int64
	models.DB.Model(&models.Ares{}).Count(&count)

	var list []models.Ares

	redis_key := "redis_list" + strconv.Itoa(page)
	Jsons, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if Jsons == "" {
		models.DB.Offset(Offset).Limit(size).Find(&list)
		jsonList, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonList, time.Hour*2)
	} else {
		json.Unmarshal([]byte(Jsons), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"list":  list,
		},
	})
	return
}
func ListXq(c *gin.Context) {
	id := c.Query("id")
	var list models.Ares

	redis_key := "redis_xq" + id
	Jsons, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if Jsons == "" {
		models.DB.Where("id=?", id).First(&list)
		jsonList, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonList, time.Hour*2)
	} else {
		json.Unmarshal([]byte(Jsons), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
func Del(c *gin.Context) {
	id := c.Query("id")
	err := models.DB.Where("id=?", id).Delete(&models.Ares{}).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "删除失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}
